package io.renren.common.utils;

/**
 * 阿拉伯数字转中文
 * @author Luo Sheng
 * @school YiBin university
 * @create 2023-05-22 20:02
 */
public class ArabicToChineseConverterUtil {
    private static final String[] CHINESE_DIGITS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] CHINESE_UNITS = {"", "十", "百", "千", "万", "十", "百", "千", "亿"};

    public static String convertToChinese(int number) {
        if (number == 0) {
            return CHINESE_DIGITS[0];
        }

        StringBuilder sb = new StringBuilder();
        int unitIndex = 0;

        while (number > 0) {
            int digit = number % 10;
            if (digit != 0) {
                sb.insert(0, CHINESE_UNITS[unitIndex]);
                sb.insert(0, CHINESE_DIGITS[digit]);
            } else if (sb.length() > 0 && !sb.substring(0, 1).equals(CHINESE_DIGITS[0])) {
                sb.insert(0, CHINESE_DIGITS[digit]);
            }
            number /= 10;
            unitIndex++;
        }

        return sb.toString();
    }
}
